package org.mineground.commands.irc;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.DecimalFormat;

import org.bukkit.entity.Player;
import org.jibble.pircbot.Colors;
import org.jibble.pircbot.User;
import org.mineground.Main;
import org.mineground.Utilities;
import org.mineground.handlers.irc.CommandExecutor;
import org.mineground.handlers.irc.UserLevel;

public class Players implements CommandExecutor {
    @Override
    public void onCommand(User sender, UserLevel level, String channel, String command, String args[]) {
	if (args.length == 1) {
	    StringBuilder query = new StringBuilder();
	    query.append("SELECT ");
	    query.append("p.last_online, ");
	    query.append("p.registered, ");
	    query.append("t.kills, ");
	    query.append("t.deaths, ");
	    query.append("t.online_time, ");
	    query.append("t.reactiontests, ");
	    query.append("t.blocks_placed, ");
	    query.append("t.blocks_removed ");
	    query.append("FROM lvm_players p ");
	    query.append("LEFT JOIN lvm_player_settings s ON p.player_id = s.player_id ");
	    query.append("LEFT JOIN lvm_player_statistics t ON p.player_id = t.player_id ");
	    query.append("WHERE p.login_name = ?");

	    try {
		PreparedStatement statement = Main.getInstance().getDatabaseHandler().getConnection().prepareStatement(query.toString());
		statement.setString(1, args[0]);

		if (!statement.execute()) {
		    Main.getInstance().getIRCHandler().sendMessage(channel, args[0] + " is not registered with Las Venturas Mineground.");
		    return;
		}

		ResultSet result = statement.getResultSet();
		result.next();

		int kills = result.getInt(3);
		int deaths = result.getInt(4);
		int reactionTests = result.getInt(6);
		int blocksPlaced = result.getInt(7);
		int blocksRemoved = result.getInt(8);
		long onlineTime = result.getLong(5);
		Date registered = result.getDate(2);
		Date lastOnline = result.getDate(1);
		DecimalFormat format = new DecimalFormat("#0.00");

		java.util.Date now = new java.util.Date();
		java.util.Date dif = new java.util.Date(registered.getTime());

		StringBuilder out = new StringBuilder();
		out.append(args[0]);

		/*if (!nick.equals(args[0])) {
		    out.append(", who is also known as ");
		    out.append(nick);
		    out.append(",");
		}*/

		out.append(" has been online for ");
		out.append(Utilities.formatTime(onlineTime));
		out.append(". ");
		out.append("The player has killed ");
		out.append(kills);
		out.append(" living entities so far, and has died ");
		out.append(deaths);
		out.append(" times him/herself, which makes a ratio of ");

		if (deaths > 0)
		    out.append(format.format(kills / (double) deaths));
		else
		    out.append(format.format((double) kills));

		out.append(".");
		out.append(" He/she won ");
		out.append(reactionTests);
		out.append(" reactiontests, placed ");
		out.append(blocksPlaced);
		out.append(" blocks and broke ");
		out.append(blocksRemoved);
		out.append(". ");
		out.append(args[0]);
		out.append(" joined the server for the first time at ");
		out.append(registered.toString());

		if (Utilities.daysBetween(dif, now) > 0) {
		    out.append(" (");
		    out.append(Utilities.daysBetween(dif, now));
		    out.append(" days ago)");
		}

		out.append(" and was last online at ");
		out.append(lastOnline.toString());

		dif = new java.util.Date(lastOnline.getTime());
		if (Utilities.daysBetween(dif, now) > 0) {
		    out.append(" (");
		    out.append(Utilities.daysBetween(dif, now));
		    out.append(" days ago).");
		} else
		    out.append(".");

		Main.getInstance().getIRCHandler().sendMessage(channel, out.toString());
                statement.close();
	    }

	    catch (Exception e) {
		Main.getInstance().getIRCHandler().sendMessage(channel, args[0] + " is not registered with Las Venturas Mineground.");
	    }

	    return;
	}

	String playersOnline;
        StringBuilder playersOnlineBuilder = new StringBuilder();
        
	Player[] onlinePlayers;
	onlinePlayers = Main.getInstance().getServer().getOnlinePlayers();

	for (Player onlinePlayer : onlinePlayers) {
            playersOnlineBuilder.append(Utilities.fixName(onlinePlayer));
            playersOnlineBuilder.append(Colors.NORMAL);
            playersOnlineBuilder.append(", ");
	}

	if (onlinePlayers.length == 0) {
	    playersOnline = Colors.OLIVE + "Online Players: " + Colors.NORMAL + "Nobody is Minecrafting right now.";
        }
        
        else {
	    playersOnline = Colors.OLIVE + Colors.BOLD + "Online Players " + Colors.NORMAL + "(" + onlinePlayers.length + "): " + playersOnlineBuilder.toString().substring(0, playersOnlineBuilder.toString().length() - 2);
        }
            
	Main.getInstance().getIRCHandler().sendMessage(channel, playersOnline);
    }
}
